﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// Player behavior.
/// </summary>
public class PlayerBehavior : MonoBehaviour
{
	public List<TokenPiece>	Tokens;
	public GameObject		PiecesPrefab; 				// Prefab

	void Start()
	{

	}

	/// <summary>
	/// Handles the friendly token selected.
	/// </summary>
	/// <returns><c>true</c>, if friendly token selected was handled, <c>false</c> otherwise.</returns>
	/// <param name="token">Token.</param>
	public bool HandleFriendlyTokenSelected (TokenPiece friendlyToken)
	{
		return true;
	}

	/// <summary>
	/// Handles the friendly token move to position.
	/// </summary>
	/// <returns><c>true</c>, if friendly token move to position was handled, <c>false</c> otherwise.</returns>
	/// <param name="token">Token.</param>
	public bool HandleFriendlyTokenMoveToPosition(TokenPiece friendlyToken, int friendlyBoardPosition)
	{
		return true;
	}

	/// <summary>
	/// Handles the enemy token attacked.
	/// </summary>
	/// <returns><c>true</c>, if enemy token attacked was handled, <c>false</c> otherwise.</returns>
	/// <param name="token">Token.</param>
	public bool HandleEnemyTokenAttacked(TokenPiece friendlyToken, ref TokenPiece enemyToken, bool isMoveAttack = true)
	{
		if (isMoveAttack && friendlyToken._movement != 0) 
		{
			// cannot "run over" a general
			if (enemyToken.TokenID == ID.ID_GENERAL)
				return false;

			// defense soldiers cannot attack
			if (friendlyToken.TokenID == ID.ID_SOLDIER && enemyToken.IsSoldierInDefenseMode == true)
				return false;

			return true;
		} 
		else if (friendlyToken._attack != 0) 
		{
			// temporary
			int NUMCOLS = 9;

			// TODO: attack stuff!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			int attackDeduction = GameHelper.CalculateAbsoluteDistance(friendlyToken.BoardPosition%NUMCOLS,friendlyToken.BoardPosition/NUMCOLS,
			                                                           enemyToken.BoardPosition%NUMCOLS,enemyToken.BoardPosition/NUMCOLS);
			if(attackDeduction > friendlyToken._attack)
				return false;
			
			friendlyToken._attack -= attackDeduction;
			

			
			// player cannot move this action
			friendlyToken._movement = 0;
			

		}
		return false;

	}

	/// <summary>
	/// Handles the friendly token special ability.
	/// </summary>
	/// <returns><c>true</c>, if friendly token special ability was handled, <c>false</c> otherwise.</returns>
	/// <param name="token">Token.</param>
	public bool HandleFriendlyTokenSpecialAbility(TokenPiece friendlyToken)
	{
		if (friendlyToken.TokenID == ID.ID_SOLDIER) 
		{
			friendlyToken.SwitchStance();
		} 
		else if (friendlyToken.TokenID == ID.ID_CANNON) 
		{
			// the rules have an issue
			// this requires 2 friendlies adjacent to the cannon
			// it uses up the actions for both friendlies
			// if there is more than 2 friendlies, what then?
			// so for now, it will disable all tokens
			//List<TokenPiece> friendlies = FindFriendliesAdjacent(token);
			
			// ready the cannon
			//friendlyToken.IsCannonReady = true;
			
			// disable all tokens
			//if(friendlies.Count >= 2)
			//{
				//foreach(TokenPiece friendly in friendlies)
				//{
					//friendly._attack = 0;
					//friendly._movement = 0;
				//}
			//}
		}

		return true;
	}

	/// <summary>
	/// Resets the tokens.
	/// </summary>
	/// <returns><c>true</c>, if tokens were reset, <c>false</c> otherwise.</returns>
	public void ResetTokens()
	{
		foreach (TokenPiece token in Tokens) 
		{
			token.Reset();
		}
	}

	/// <summary>
	/// Removes the tokens.
	/// </summary>
	public void RemoveTokens()
	{
		foreach (TokenPiece token in Tokens) 
		{
			Destroy(token.gameObject);
		}
		Tokens.Clear ();
		Tokens = new List<TokenPiece> ();
	}
}
